// 配置文档：https://github.com/nuysoft/Mock/wiki
import Mock from 'mockjs'
import ASC from '/app_settings_core'
import Logger from '~/utils/logger'
import { notEmpty } from '~/utils/common'

import resercedMockData from './reserved'
import customMockData from '@/http/mock/index'

const methods = ['get', 'head', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'patch']

/**
 * 加载脚手架保留API的Mock挡板
 * @param {*} mockAll       true-开启所有挡板
 * @param {*} mockOnDemand  true-按需开启挡板
 */
function loadReservedMock(mockAll, mockOnDemand) {
  for (const apiName in resercedMockData) {
    if (notEmpty(ASC.settings.http.api.reserved[apiName])) {
      const api = ASC.settings.http.api.reserved[apiName]
      const mock = api.mock
      const mockData = resercedMockData[apiName]
      if ((mockAll || (mockOnDemand && mock)) && notEmpty(mockData)) {
        let baseUrl = ASC.settings.http.baseUrl
        if (baseUrl.endsWith('/')) {
          baseUrl = baseUrl.slice(0, baseUrl.length - 1)
        }
        let pathUrl = api.url
        if (pathUrl[0] === '/') {
          pathUrl = pathUrl.slice(1, pathUrl.length)
        }
        const url = baseUrl + '/' + pathUrl
        const type = api.method && methods.includes(api.method.toLowerCase()) ? api.method : 'get'

        Mock.mock(new RegExp(url), type, options => {
          options['data'] = options.body ? JSON.parse(options.body) : null
          delete options.body
          Logger.log('%cmock拦截, 请求: ', 'color:blue', options)
          Logger.log('%cmock拦截, 响应: ', 'color:blue', mockData)
          return mockData
        })
      }
    }
  }
}

/**
 * 加载自定义API的Mock挡板
 * @param {*} mockAll       true-开启所有挡板
 * @param {*} mockOnDemand  true-按需开启挡板
 */
function loadCustomMock(mockAll, mockOnDemand) {
  for (const moduleName in customMockData) {
    if (notEmpty(ASC.settings.http.api.custom[moduleName])) {
      for (const apiName in customMockData[moduleName]) {
        if (notEmpty(ASC.settings.http.api.custom[moduleName][apiName])) {
          const api = ASC.settings.http.api.custom[moduleName][apiName]
          const mock = api.mock
          const mockData = customMockData[moduleName][apiName]

          if ((mockAll || (mockOnDemand && mock)) && notEmpty(mockData)) {
            let baseUrl = ASC.settings.http.baseUrl
            if (baseUrl.endsWith('/')) {
              baseUrl = baseUrl.slice(0, baseUrl.length - 1)
            }
            let pathUrl = api.url
            if (pathUrl[0] === '/') {
              pathUrl = pathUrl.slice(1, pathUrl.length)
            }
            const url = baseUrl + '/' + pathUrl
            const type = api.method && methods.includes(api.method.toLowerCase()) ? api.method : 'get'

            Mock.mock(new RegExp(url), type, options => {
              options['data'] = options.body ? JSON.parse(options.body) : null
              delete options.body
              Logger.log('%cmock拦截, 请求: ', 'color:blue', options)
              Logger.log('%cmock拦截, 响应: ', 'color:blue', mockData)
              return mockData
            })
          }
        }
      }
    }
  }
}

export function initMock() {
  const mockAll = ASC.settings.http.mockAll
  const mockOnDemand = ASC.settings.http.mockOnDemand
  if (mockAll || mockOnDemand) {
    loadReservedMock(mockAll, mockOnDemand)
    loadCustomMock(mockAll, mockOnDemand)
  }
}
